<script>
        var timer,t,tt = null,expTime = 0,showTimeTips=true;
            timer = function () {
                var scanUrl = store.get('qr_url');
                if(!(scanUrl == undefined || scanUrl.create_time == undefined ||
                    scanUrl.expire_time == undefined || scanUrl.create_time*1000+scanUrl.expire_time*1000<(new Date()).getTime()))
                {
                    expTime = parseInt(((scanUrl.create_time*1000+scanUrl.expire_time*1000) - (new Date()).getTime())/1000); 
                }
        
                layui.use('util', function(){
                    var util = layui.util;
        
                    var endTime = new Date().getTime()+expTime*1000 ,serverTime = new Date().getTime(); 
                        util.countdown(endTime, serverTime, function(date, serverTime, myTimer){
                        
                            tt = myTimer;
                            // console.log("请使用微信扫码登录，二维码有效期是："+formatSeconds(expTime-1));
                            if(showTimeTips){
                                $(".qrcode_hover").hide();
                                layui.$('.qrcode-text').html("请使用微信扫码登录，二维码有效期是："+formatSeconds(expTime-1)).css("color","#666666");
                            }
                            if(expTime <= 1){
                                setTimeout(function(){
                                    $(".qrcode-text").html("二维码已失效，请刷新二维码").css("color", "#d4237a");
                                    $(".qrcode_hover").show();
                                },1000);
                                clearInterval(t);
                                clearTimeout(tt);
                                return false;
                            }
                        });
                });
        
                t = setInterval(function () {
                        // console.log(showTimeTips);
                        var scanUrl = store.get('qr_url');
                        if(!(scanUrl == undefined || scanUrl.create_time == undefined ||
                            scanUrl.expire_time == undefined || scanUrl.create_time*1000+scanUrl.expire_time*1000<(new Date()).getTime()))
                        {
                            expTime = parseInt(((scanUrl.create_time*1000+scanUrl.expire_time*1000) - (new Date()).getTime())/1000); 
                            if(showTimeTips){
                                $('.qrcode-text').html("请使用微信扫码登录，二维码有效期是："+formatSeconds(expTime)).css("color","#666666");
                            }
                        }
                        // console.log("请使用微信扫码登录，二维码有效期是："+formatSeconds(expTime));
                        if(expTime%3 == 0 || expTime < 5){
                        
                            var http = new Request({token_url:api.token_url,async:false,token_store_name:'scan_login_token'});
                            var e = http.request('post',api.check_scan_url+"?_="+(new Date()).getTime(),{mk:scanUrl.mk});
        
                            //登录成功
                            if(e.code == 0){
                                if(e.data.sso_token !== undefined  && e.data.sso_token !== ''){
                                    store.set('sso_token',e.data.sso_token); 
                                    store.set('qr_url',null); 
                                    $(".qrcode-text").html("已确认登录，正在跳转中...").css('color','#07c160');
                                    setTimeout(function(){
                                        window.location.href = scanUrl.go_to_url;
                                    },500);
                                    clearInterval(t);
                                    clearInterval(tt);
                                }else{
                                    $(".qrcode_hover").show();
                                    $(".qrcode-text").html("请刷新二维码重新登录！").css('color','#ff976a');
                                }
                                return false;
                            }
                        
                            //已扫码
                            if(e.code == 20001){
                                showTimeTips = false;
                                $(".qrcode_hover").show();
                                $(".qrcode-text").html(e.msg).css('color','#ff976a');
                                scanUrl.expire_time = 0;
                                store.set('qr_url',scanUrl); 
                                return false;
                            }

                             //账号已过期
                             if(e.code == 20051){
                                $(".qrcode_hover").show();
                                $(".qrcode-text").html(e.msg).css('color','#d4237a');
                                clearInterval(t);
                                clearInterval(tt);
                                scanUrl.expire_time = 0;
                                store.set('qr_url',scanUrl); 
                                return false;
                            }
        
                            //已禁用登录
                            if(e.code == 20002){
                                $(".qrcode_hover").show();
                                $(".qrcode-text").html(e.msg).css('color','#d4237a');
                                clearInterval(t);
                                clearInterval(tt);
                                scanUrl.expire_time = 0;
                                store.set('qr_url',scanUrl); 
                                return false;
                            }
        
                            //数据验证时，缺少参数
                            if(e.code == 20003){
                                $(".qrcode_hover").show();
                                $(".qrcode-text").html(e.msg).css('color','#d4237a');
                                clearInterval(t);
                                clearInterval(tt);
                                scanUrl.expire_time = 0;
                                store.set('qr_url',scanUrl); 
                                return false;
                            }
        
                            //用户取消了登录
                            if(e.code == 20007){
                                $(".qrcode_hover").show();
                                $(".qrcode-text").html(e.msg).css('color','#d4237a');
                                clearInterval(t);
                                clearInterval(tt);
                                scanUrl.expire_time = 0;
                                store.set('qr_url',scanUrl); 
                                return false;
                            }
        
                            //用户未绑定
                            if(e.code == 20004){
                                $(".qrcode_hover").show();
                                $(".qrcode-text").html(e.msg).css('color','#d4237a');
                                clearInterval(t);
                                clearInterval(tt);
                                scanUrl.expire_time = 0;
                                store.set('qr_url',scanUrl); 
                                return false;
                            }
        
                        }
        
                    }, 1000);
                }
        
            
            $(function () {
                layui.use('form', function () {
                    var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
                    form.render();
                });
               
                $("#online").attr("checked", store.get('online'));
                $("#user").val(store.get('user'));
        
                $("#submit").on("click", function (e) {
                    e.preventDefault();
                    doLogin();
                })
        
                $(".login_qrcode").on("click", function (e) {
                    $(this).hide();
                    $(".login_by_qrcode").hide();
                    $(".login_text").show();
                    $(".login_by_text").show();
                    clearInterval(t);
                });
                reQrcode();
                $(".login_text").on("click", function (e) {
                
                    $(this).hide();
                    $(".login_by_text").hide();
                    $(".login_qrcode").show();
                    $(".login_by_qrcode").show();
                    $(".qrcode_hover").hide();
        
                    if($(".qrcode-img").attr('title') !== ''){
                        var scanUrl = store.get('qr_url');
                        if(scanUrl == undefined || scanUrl.create_time == undefined ||
                            scanUrl.expire_time == undefined || scanUrl.create_time*1000+scanUrl.expire_time*1000<(new Date()).getTime())
                        {
                            $(".qrcode-text").html("请使用微信扫码").css("color","##5be62f");
                        
                        }else{
                            expTime = parseInt(((scanUrl.create_time*1000+scanUrl.expire_time*1000) - (new Date()).getTime())/1000);
                            
                        }
                        setTimeout(function(){
                                reQrcode();
                            },500);
                    }
                  
                });
        
        
                
            });
         
            /**
             * 密码登陆
             */
            function doLogin() {
                var user = $("#user").val();
                var pwd = $("#pwd").val();
                var yzm = $("#yzm").val();
                var token = $("input[name='__token__']").val();
                if (user == '' || pwd == '' || yzm == '') {
                    return false;
                }
                var http = new Request({token_url:api.token_url});
                var e = http.request('post',api.check_login_url,{ user: user, pwd: pwd, yzm: yzm, __token__: token });
                if(e.code === 0){
                    online();
                  
                    layui.use('layer', function () {
                        var layer = layui.layer;
                        layer.load(0, {shade: false})
                        layer.msg(e.msg); 
                    });
                    // console.log(api.login_success_url);
                    window.location.href = api.login_success_url ;
                }else{
                    layer.closeAll();
                    $("#yzm").val("");
                    $(".yzm-img").trigger("click");//更新验证码
                    var index = layui.use('layer', function () {
                        var layer = layui.layer;
                        layer.msg(e.msg);
                    });
                }
            }
        
            /**
             * 记住账号
             */
            function online() {
                $value = '';
                $online = false;
                if($("#online").is(':checked')){
                    $value =  $("#user").val();
                    $online = true;
                }
                store.set('user',$value);
                store.set('online',$online);
            }
        
            /**
             * 刷新二维码
             */
            function reQrcode() {
              
                //如果是过期时，有遮罩层
                $(".qrcode_hover").hide();
                showTimeTips = true;//开启倒计时
                var scanUrl = store.get('qr_url');
        
                if(scanUrl == undefined || scanUrl.create_time == undefined ||
                    scanUrl.expire_time == undefined ||  scanUrl.status !== 0 || scanUrl.create_time*1000+scanUrl.expire_time*1000<(new Date()).getTime())
                {
                    var http = new Request({token_url:api.token_url});
                    var e = http.request('get',api.qr_login_url+"?_t="+(new Date()).getTime());
                    if(e.code==0){
                        store.set('qr_url',e.data); 
                    }else{  
                        $(".qrcode-text").html(e.msg).css('color','#d4237a');
                        return false;
                    }
                }
        
                scanUrl = store.get('qr_url');
            
                $(".qrcode-img").empty();
                new QRCode("qrcode-img", {
                    text: scanUrl.url ,
                    width: 160,
                    height: 160,
                    colorDark: "#000000",
                    colorLight: "#ffffff",
                    correctLevel: QRCode.CorrectLevel.H
                });
        
                $(".qrcode_hover").hide();
                
                clearInterval(t);
                timer(); 
            }
        
            /**更新验证码*/
            function showVerify(obj) {
                var imgURL = "{:url('login/login/verify')}?" + Math.random();
                $(obj).attr("src", imgURL);
            }
       
</script>